<script setup>
import { ref } from 'vue'
import { artGetChannelService, artDelChannelService } from '@/api/article.js'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from '@/views/article/components/ChannelEdit.vue'

const channelList = ref([])
const loading = ref(false)
const dialog = ref()

// 获取文章分类
const getChannelList = async () => {
  // 请求开始,放加载效果
  loading.value = true

  const res = await artGetChannelService()
  channelList.value = res.data.data

  // 加载结束关闭动画
  loading.value = false
}
getChannelList()

const onEditChannel = (row) => {
  dialog.value.open(row)
}
const onDeleteChannel = async (row) => {
  // 确认删除提示框confirm(提示框文本, 提示框的标题, {类型, 肯定按钮文本, 否定按钮文本})
  await ElMessageBox.confirm('你确认要删除这个分类嘛?', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelChannelService(row.id)
  ElMessage.success('删除成功')
  // 删除完,重新获取数据
  getChannelList()

  // 可以在点击删除的时候弹出确认框,用户真的确认要删除再进行删除,防止误删
}
const onAddChannel = () => {
  // dialogVisible = true
  dialog.value.open({})
}

const onSuccess = () => {
  // 编辑或添加完重新获取数据
  getChannelList()
}
</script>
<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button type="primary" @click="onAddChannel">添加分类</el-button>
    </template>
    <el-table
      v-loading="loading"
      :data="channelList"
      stripe
      style="width: 100%"
    >
      <el-table-column type="index" label="序号" width="180" />
      <el-table-column prop="cate_name" label="分类名称" />
      <el-table-column prop="cate_alias" label="分类别名" />
      <el-table-column label="操作" width="180">
        <!-- row是每一项  $index是下标 -->
        <template #default="{ row, $index }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditChannel(row, $index)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDeleteChannel(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
  </page-container>
</template>

<style lang="less" scoped></style>
